using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace Triton.Common
{
	public static class Utilities
	{
		[Serializable]
		[CompilerGenerated]
		private sealed class Class238<T>
		{
			public static readonly Class238<T> Instance9 = new Class238<T>();

			public static Func<Assembly, IEnumerable<Type>> Instance9__0_0;

			public static Func<Assembly, Type, Class225<Assembly, Type>> Instance9__0_1;

			public static Func<Type, bool> Instance9__0_3;

			public static Func<Class225<Assembly, Type>, bool> Instance9__0_2;

			public static Func<Class225<Assembly, Type>, T> Instance9__0_4;

			internal IEnumerable<Type> method_0(Assembly assembly_0)
			{
				return assembly_0.GetTypes();
			}

			internal Class225<Assembly, Type> method_1(Assembly assembly_0, Type type_0)
			{
				return new Class225<Assembly, Type>(assembly_0, type_0);
			}

			internal bool method_2(Class225<Assembly, Type> class225_0)
			{
				return class225_0.type.GetInterfaces().Any((Type type_0) => type_0 == typeof(T));
			}

			internal bool method_3(Type type_0)
			{
				return type_0 == typeof(T);
			}

			internal T method_4(Class225<Assembly, Type> class225_0)
			{
				return (T)Activator.CreateInstance(class225_0.type);
			}
		}

		private static string string_0 = smethod_0();

		public static string AssemblyDirectory
		{
			get
			{
				return string_0;
			}
			set
			{
				string_0 = value;
			}
		}

		public static bool IsWindowsXp => Environment.OSVersion.Version.Major == 5;

		public static IEnumerable<T> FindInterfaceImplementations<T>()
		{
			return from assembly_0 in AppDomain.CurrentDomain.GetAssemblies()
				from type_0 in assembly_0.GetTypes()
				select new Class225<Assembly, Type>(assembly_0, type_0) into class225_0
				where class225_0.type.GetInterfaces().Any((Type type_0) => type_0 == typeof(T))
				select (T)Activator.CreateInstance(class225_0.type);
		}

		private static string smethod_0()
		{
			try
			{
				Assembly executingAssembly = Assembly.GetExecutingAssembly();
				if (string.IsNullOrEmpty(executingAssembly.Location))
				{
					return Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), "Hearthstone_Data\\Managed");
				}
				return Path.GetDirectoryName(executingAssembly.Location);
			}
			catch
			{
				return "";
			}
		}

		public static DateTime ConvertFromUnixTimestamp(ulong timestamp)
		{
			return new DateTime(1970, 1, 1, 0, 0, 0, 0).AddSeconds(timestamp);
		}

		public static int ConvertToUnixTimestamp(DateTime date)
		{
			DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0);
			return (int)(date - dateTime).TotalSeconds;
		}

		public static DateTime PerformanceCounterToDateTime(ulong performanceCounter)
		{
			return DateTime.Now.Subtract(TimeSpan.FromMilliseconds(Environment.TickCount)).AddMilliseconds(performanceCounter);
		}

		[Conditional("DEBUG")]
		public static void PrintStackTrace(string reason = "Debug")
		{
			StackFrame[] frames = new StackTrace(fNeedFileInfo: true).GetFrames();
			for (int i = 1; i < System.Math.Min(frames.Length, 10); i++)
			{
			}
		}

		public static string GetObjectString(object obj, string nullRet)
		{
			if (obj == null)
			{
				return nullRet;
			}
			return obj.ToString();
		}

		public static string FormatString([Localizable(true)] string format, params object[] args)
		{
			try
			{
				return string.Format(Thread.CurrentThread.CurrentUICulture, format, args);
			}
			catch (Exception ex)
			{
				return $"FS_EMPTY {ex.GetType().Name} {format.ToString()}";
			}
		}

		internal static string smethod_1(CompilerResults compilerResults_0)
		{
			StringBuilder stringBuilder = new StringBuilder();
			foreach (CompilerError error in compilerResults_0.Errors)
			{
				if (!error.IsWarning)
				{
					stringBuilder.AppendLine(string.Format("File: {0} Line: {1} {3}: {2}", Path.GetFileName(error.FileName), error.Line, error.ErrorText, error.IsWarning ? "Warning" : "Error"));
				}
			}
			return stringBuilder.ToString();
		}

		public static int FNV1a(int value)
		{
			byte[] bytes = BitConverter.GetBytes(value);
			return bytes[3] ^ (16777619 * (bytes[2] ^ (16777619 * (bytes[1] ^ (16777619 * (bytes[0] ^ (16777619 * (value ^ -2128831035))))))));
		}

		public static uint FNV1a(string value)
		{
			byte[] bytes = Encoding.Unicode.GetBytes(value + "\0");
			uint num = 2166136261u;
			int num2 = 0;
			int num3 = value.Length * 2;
			do
			{
				ushort num4 = BitConverter.ToUInt16(bytes, num2);
				num2 += num3 / 10 + 1;
				num = num4 ^ (16777619 * num);
			}
			while (num2 < num3);
			return num;
		}
	}
}
